package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.CustomException;

import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    public void save(SetmealDto setmealDto) {
        setmealMapper.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishMapper.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Setmeal> setmealList = setmealMapper.findByName(name);

        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);

            Category category = categoryMapper.findById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        Page<SetmealDto> pageResult = new Page<>(setmealDtoList,page,pageSize,pageInfo.getTotal());
        return pageResult;
    }

    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void deleteByIds(List<Long> ids) {
       int count = setmealMapper.findSetmealWithStatus(ids);

       if(count>0){
           throw new CustomException("该套餐还没停售，无法删除");
       }

       setmealMapper.deleteByIds(ids);

       setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    @Cacheable(value = "setmeal",key="#categoryId+'_'+#status")
    public List<Setmeal> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList = setmealMapper.list(categoryId,status);
        return setmealList;
    }

    @Override
    public SetmealDto get(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        List<SetmealDish> setmealDishes =setmealDishMapper.getBysetmealId(id);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void updateWithSetmealDish(SetmealDto setmealDto) {

        setmealMapper.updateSetmeal(setmealDto);
        setmealDishMapper.deleteBySetmealId(setmealDto.getId());

        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
                setmealDish.setUpdateTime(LocalDateTime.now());
                setmealDish.setUpdateUser(setmealDto.getUpdateUser());
                setmealDish.setCreateTime(LocalDateTime.now());
                setmealDish.setCreateUser(setmealDto.getCreateUser());
                setmealDish.setSort(0);
                return setmealDish;
            }).collect(Collectors.toList());

        setmealDishMapper.saveBatch(setmealDishes);
    }

    @Override
    public void updateStatus(List<Long> ids, SetmealDto setmealDto, Integer status) {
        Setmeal setmeal = new Setmeal();
        setmeal.setUpdateTime(setmealDto.getUpdateTime());
        setmeal.setUpdateUser(setmealDto.getUpdateUser());

        SetmealDish setmealDish = new SetmealDish();
        setmealDish.setUpdateTime(setmealDto.getUpdateTime());
        setmealDish.setUpdateUser(setmealDto.getUpdateUser());

        setmealMapper.updateStatus(ids,setmeal,status);
        setmealDishMapper.updateBySetmealIds(ids,setmealDish);
    }


}


